package cn.snmp.mutual.nms.mibs;

import java.io.*;
import java.util.*;

import cn.snmp.mutual.uk.stack.AsnObject;

public class MibNode implements Serializable, MIBConstants
{
    protected Hashtable m_propreties;
    protected MibModule m_module;
    
    public MibNode(final MibModule module, final Hashtable proteties) {
        this.m_propreties = null;
        this.m_module = null;
        this.m_module = module;
        this.m_propreties = proteties;
    }
    
    public void clear() {
        if (this.m_propreties != null) {
            this.m_propreties.clear();
        }
        this.m_propreties = null;
        this.m_module = null;
    }
    
    public Hashtable getPropreties() {
        return this.m_propreties;
    }
    
    public MibModule getModule() {
        return this.m_module;
    }
    
    public LeafSyntax getSyntax() {
        final Hashtable syntax = (Hashtable)this.m_propreties.get("Syntax");
        if (syntax == null) {
            return null;
        }
        return new LeafSyntax(syntax);
    }
    
    public String getLabel() {
        return (String)this.m_propreties.get("Label");
    }
    
    public int getAccess() {
        return Integer.parseInt((String)this.m_propreties.get("Access"));
    }
    
    public String getParent() {
        return (String)this.m_propreties.get("Parent");
    }
    
    public boolean isIndex() {
        final String proprety = (String)this.m_propreties.get("isIndex");
        return proprety != null && proprety.equalsIgnoreCase("true");
    }
    
    public boolean isLeaf() {
        final String proprety = (String)this.m_propreties.get("isLeaf");
        return proprety != null && proprety.equalsIgnoreCase("true");
    }
    
    public boolean isTable() {
        final String proprety = (String)this.m_propreties.get("isTable");
        return proprety != null && proprety.equalsIgnoreCase("true");
    }
    
    public boolean isTableColumn() {
        final String proprety = (String)this.m_propreties.get("isTableColumn");
        return proprety != null && proprety.equalsIgnoreCase("true");
    }
    
    public boolean isTableEntry() {
        final String proprety = (String)this.m_propreties.get("isTableEntry");
        return proprety != null && proprety.equalsIgnoreCase("true");
    }
    
    public String getOIDString() {
        return (String)this.m_propreties.get("OIDString");
    }
    
    public String getNumberedOIDString() {
        return (String)this.m_propreties.get("NumberedOIDString");
    }
    
    public String getStatus() {
        return (String)this.m_propreties.get("Status");
    }
    
    public String getDescription() {
        return (String)this.m_propreties.get("Description");
    }
    
    public String getDefval() {
        return (String)this.m_propreties.get("Defval");
    }
    
    public String getNotificationsNames() {
        return (String)this.m_propreties.get("NotificationsNames");
    }
    
    public Vector getObjects() {
        Vector vObjects = null;
        try {
            vObjects = (Vector)this.m_propreties.get("Objects");
        }
        catch (Exception ex) {}
        return vObjects;
    }
    
    public Vector getChildList() {
        final Vector children = new Vector();
        try {
            if (this.m_propreties.get("Children") != null) {
                final Vector names = (Vector)this.m_propreties.get("Children");
                for (int i = 0; i < names.size(); ++i) {
                    final String name = (String) names.get(i);
                    final MibNode node = this.m_module.getMibNode(name);
                    if (node != null) {
                        children.add(node);
                    }
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return children;
    }
    
    public Vector getChildNameList() {
        if (this.m_propreties.get("Children") != null) {
            final Vector names = (Vector)this.m_propreties.get("Children");
            return names;
        }
        return new Vector();
    }
    
    public Vector getAllChildren() {
        try {
            final Vector vAllChildren = new Vector();
            final Vector<MibNode> children = (Vector<MibNode>)this.getChildList().clone();
            Vector vIndexNames = new Vector();
            final Vector vChildrenNames = new Vector();
            for (int i = 0; i < children.size(); ++i) {
                vChildrenNames.add(children.elementAt(i).getLabel());
            }
            final Object oIndexNames = this.m_propreties.get("IndexNames");
            if (oIndexNames != null) {
                vIndexNames = (Vector)oIndexNames;
            }
            for (int j = 0; j < vIndexNames.size(); ++j) {
                final String strNodeName = (String)vIndexNames.get(j);
                Object node = this.m_module.getMibNode(strNodeName);
                if (node == null) {
                    node = this.m_module.getMibOperations().getMibNode(strNodeName);
                }
                if (node != null) {
                    if (!vChildrenNames.contains(strNodeName)) {
                        vAllChildren.add(node);
                    }
                }
            }
            for (int j = 0; j < children.size(); ++j) {
                vAllChildren.add(children.elementAt(j));
            }
            return vAllChildren;
        }
        catch (Exception e) {
            return this.getChildList();
        }
    }
    
    public Vector getAllChildrenNameList() {
        try {
            final Vector vAllChildren = new Vector();
            final Vector children = this.getChildNameList();
            Vector vIndexNames = new Vector();
            final Object oIndexNames = this.m_propreties.get("IndexNames");
            if (oIndexNames != null) {
                vIndexNames = (Vector)oIndexNames;
            }
            for (int i = 0; i < vIndexNames.size(); ++i) {
                if (!children.contains(vIndexNames.elementAt(i))) {
                    vAllChildren.add(vIndexNames.elementAt(i));
                }
            }
            for (int i = 0; i < children.size(); ++i) {
                vAllChildren.add(children.elementAt(i));
            }
            return vAllChildren;
        }
        catch (Exception e) {
            return this.getChildNameList();
        }
    }
    
    public int getSubid() {
        try {
            final String oid = this.getNumberedOIDString();
            final String subid = oid.substring(oid.lastIndexOf(".") + 1);
            if (AsnObject.debug > 6) {
                System.out.println("-------------> oid is " + oid);
            }
            return Integer.parseInt(subid);
        }
        catch (Exception e) {
            if (AsnObject.debug > 6) {
                e.printStackTrace();
            }
            return -1;
        }
    }
    
    public MibNode getChild(final int i) {
        try {
            final Vector childList = this.getChildList();
            final Object obj = null;
            for (int j = childList.size(), k = 0; k < j; ++k) {
                final MibNode mibnode = (MibNode)childList.elementAt(k);
                if (mibnode.getSubid() == i) {
                    return mibnode;
                }
            }
            return null;
        }
        catch (Exception e) {
            return null;
        }
    }
    
    public String getChildName(final int i) {
        try {
            if (this.m_propreties.get("Children") != null) {
                final Vector names = (Vector)this.m_propreties.get("Children");
                return (String) names.get(i - 1);
            }
            return null;
        }
        catch (Exception e) {
            return null;
        }
    }
}
